﻿using WAU.Common.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.Extensions;
using System.Text;

namespace WAU.Controllers
{
    /// <summary>
    /// 测试用控制器（用户测试 .NET Core 应用是否部署成功）
    /// </summary>
    [ApiController]
    [Route("/api/[controller]/[action]")]
    public class HelloWorldController : Controller
    {
        /// <summary>
        /// HelloWorld
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult HelloWorld()
        {
            return Ok(new Result
            {
                Success = true,
                Data = "Hello World!"
            });
        }

        #region 生成各种状态码

        /// <summary>
        /// 生成 204 状态码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Generate204()
        {
            return NoContent();
        }

        /// <summary>
        /// 生成 401 状态码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Generate401()
        {
            return Unauthorized();
        }

        /// <summary>
        /// 生成 403 状态码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Generate403()
        {
            return Forbid();
        }

        /// <summary>
        /// 生成 500 状态码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Generate500()
        {
            return StatusCode(500);
        }

        #endregion

        /// <summary>
        /// Hello
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [AntiRepeatedCommit]
        public ActionResult AntiRepeatedHello()
        {
            Console.WriteLine("还是执行了");
            return Ok(new Result
            {
                Success = true,
                Data = "Hello World!"
            });
        }

        /// <summary>
        /// Echo
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Echo()
        {
            string ip = HttpContext.Connection.RemoteIpAddress.ToString();
            string url = HttpContext.Request.Host + HttpContext.Request.Path;
            string queryString = HttpContext.Request.QueryString.ToString();

            string bodyString;
            using(StreamReader sr = new StreamReader(HttpContext.Request.Body)) 
            {
                bodyString = await sr.ReadToEndAsync();
            }

            string returnString = $"源 IP 地址为 {ip}\n" +
                $"Url 为 {url}\n" +
                $"queryString 为 {queryString}\n" +
                $"请求体 Body 内容为\n {bodyString}";

            return Ok(returnString);            
        }

        /// <summary>
        /// HelloException
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult HelloException()
        {
            int a = 0;
            int b = 100 / a;

            return Ok(b);
        }
    }
}
